package operationgenerator;

import java.awt.*;
import javax.swing.*;
import java.lang.Math;


public class OperationGenerator {

    public static void main(String[] args) {
        OperationGenerator test = new OperationGenerator();
        test.window();
    }

    private TextField textfield;//算式个数输入框
    private int operationNum = 0;//运算式子个数
    private TextField[] answerFields;//答案填写框
    private TextField[] flagFields;//对错标识框
    //实例化一个窗口
    public void window() {
        //实例化对象的同时设置窗口名字
        //定义组件
        JFrame frame = new JFrame("OperationGenerator");
        Panel panel1 = new Panel();//声明第一行的面板1
        Panel panel2 = new Panel();//面板二：用来装生成的算式
        //设置面板的布局方式
        frame.setLayout(null);
        frame.setBounds(100,100,500,600);
        frame.setBackground(new Color(86, 211, 214));
        panel1.setBounds(0,0,500,50);
        panel2.setBounds(0,100,500,500);

        //实例化button按钮并设置按钮显示的名字
        textfield = new TextField(15);
        JButton button1 = new JButton("clear");
        //按键:clear与create
        JButton button2 = new JButton("create");
        textfield.setFont(new Font("Nimbus Mono L", Font.ITALIC, 15));

        //元素添加进入panel1
        panel1.add(textfield);
        panel1.add(button1);
        panel1.add(button2);

        //将面板添加进布局
        frame.add(panel1);
        frame.add(panel2);

        /*
         * @description 使用匿名内部类实现监听器,点击“clear”按钮实现清楚panel2中的所有内容
         * @author HighJ
         * @date 2022/4/18 16:24
         */
        button1.addActionListener(e -> {
            panel2.removeAll();
            panel2.repaint();
        });
        /*
         * @description 使用匿名内部类实现监听器,点击“creat”按钮实现生成四则运算，同时将其显示到panel2上
         * @author HighJ
         * @date 2022/4/18 16:24
         */
        button2.addActionListener(e -> {
            String num = textfield.getText();//此处有待完善，若用户输入不规范的处理
            try {
                operationNum = Integer.parseInt(num);//读取需要生成的算式个数
            }
            catch (NumberFormatException nfe){
                nfe.printStackTrace();
            }

            JPanel panel3 = updatePanel2(operationNum);
            panel2.removeAll();//通过删除组件重绘来更新panel2中的内容
            panel2.repaint();
            //更新panel2
            panel2.add(panel3);
            panel2.revalidate();
        });
        //设置窗口可见
        frame.setVisible(true);
        //设置窗口默认显示位置（居中）
        frame.setLocationRelativeTo(null);
        //设置面板默认点击关闭生效
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

  /*
  * @Description:算式生成函数，通过读取算式个数输入框中的数字生成指定个数的四则运算式子
  * @Param: num
  * @Return: String[]
  * @Author: HighJ
  * @Date Created in 2022.04.18
  */

    public static String[] Create(int num){
        String[] operator = new String[num*2];
        int[] randomNum1 = new int[num];//保存生成1~100之间的数字
        int[] randomNum2 = new int[num];
        int[] randomOperator = new int[num];//保存随机的运算符
        double[] operatorAnswer = new double[num];//保存运算答案
        for(int i = 0;i<num;i++) {
            randomNum1[i] = (int)(Math.random()*(1-100)+100);//生成1-100随机数
            randomNum2[i] = (int)(Math.random()*(1-100)+100);
            randomOperator[i] = (int)(Math.random()*(1-4)+4);
        }
        for(int i = 0;i<num;i++){//算出答案
            int a = randomOperator[i];
            switch (a){
                case 0:
                    operatorAnswer[i] = randomNum1[i] + randomNum2[i];//随机生成四则运算中的一种：分别用1-4代表+-*/
                    break;
                case 1:
                    operatorAnswer[i] = randomNum1[i] - randomNum2[i];
                    break;
                case 2:
                    operatorAnswer[i] = randomNum1[i] * randomNum2[i];
                    break;

                default:
                    operatorAnswer[i] = (float)randomNum1[i] / randomNum2[i];
                    break;
            }

        }
        for(int i = 0;i<num;i++) {//生成算式的字符串
            int a = randomOperator[i];
            switch (a){
                case 0:
                    operator[i] = "" + randomNum1[i] + "+" + randomNum2[i];
                    break;
                case 1:
                    operator[i] = "" + randomNum1[i] + "-" + randomNum2[i];
                    break;
                case 2:
                    operator[i] = "" + randomNum1[i] + "*" + randomNum2[i];
                    break;
                default:
                    operator[i] = "" + randomNum1[i] + "/" + randomNum2[i];
                    break;
            }
            operator[i+num] = ""+(int)operatorAnswer[i];
        }
        return operator;
    }

    //Panel更新函数

    /*
     * @description
     * @param null
     * @author HighJ
     * @date 2022/4/18 22:32
     */

    public JPanel updatePanel2(int operationNum){
        JPanel resultPanel = new JPanel();
        JButton submit = new JButton("submit");//提交按钮
        submit.addActionListener(e -> {
            for(int i=0;i<operationNum;i++ ){
                String answer = answerFields[i].getText();
                String flag = flagFields[i].getText();
                if(answer.equals(flag)){
                    flagFields[i].setVisible(true);
                    flagFields[i].setText("T");
                }
                else{
                    flagFields[i].setVisible(true);
                    flagFields[i].setText("X");
                }
            }
        });
        //显示运算式子
        JTextField[] operatorFields = new JTextField[operationNum];//初始化算式显示位置
        answerFields = new TextField[operationNum];//初始化答案填写框
        flagFields = new TextField[operationNum];//初始化对错标识框
        resultPanel.setLayout(new GridLayout(operationNum+1,4));//3n行3列的布局
        Font font = new Font("Nimbus Mono L", Font.ITALIC, 30);
        //保存运算式子
        String[] operator = Create(operationNum);//调用Create函数生成算式
        for(int i = 0;i<operationNum;i++){
            operatorFields[i] = new JTextField();
            this.answerFields[i] = new TextField(4);
            this.flagFields[i] = new TextField(1);//判断答案是否正确的标签
            operatorFields[i].setFont(font);
            operatorFields[i].setEditable(false);
            this.flagFields[i].setEditable(false);
            this.flagFields[i].setText(operator[i+operationNum]);
            this.flagFields[i].setFont(font);
            this.flagFields[i].setVisible(false);
            this.answerFields[i].setFont(font);
            operatorFields[i].setText( operator[i] + "=");
            resultPanel.add(operatorFields[i]);
            resultPanel.add(this.answerFields[i]);
            resultPanel.add(this.flagFields[i]);
        }
        resultPanel.add(submit);
        return resultPanel;
    }
}
